<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统-演示</div>

            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <!-- <el-form-item prop="">
                    <label>登录账户：&nbsp;</label>
                    <el-select v-model="value" placeholder="选择登录账户" @change="changeUser()">
                        <el-option v-for="item in users" :key="item.value" :label="item.label" :value="item.value">
                            <span style="float: left">{{ item.label }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                        </el-option>
                        <el-button slot="prepend" icon="el-icon-lx-lock">密&nbsp;&nbsp;&nbsp;码</el-button>
                    </el-select>
                </el-form-item>

                <el-form-item prop="tenantname">
                    <el-input v-model="param.tenantname" placeholder="请输入租户名">
                        <el-button slot="prepend" icon="el-icon-lx-people">租户名</el-button>
                    </el-input>
                </el-form-item> -->

                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="请输入用户名" value="admin">
                        <el-button slot="prepend" icon="el-icon-lx-people">用户名</el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="param.password" @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lx-lock">密&nbsp;&nbsp;&nbsp;码</el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
                <p class="login-tips">
                    <!-- <el-radio v-model="radio" label="1" @change="changeUser()">租户登录</el-radio>
                    <el-radio v-model="radio" label="2" @change="changeUser()">宿主登录</el-radio> -->
                </p>
            </el-form>
        </div>
    </div>
</template>

<script>
import axios from 'axios';
export default {
    data: function () {
        return {
            radio: '1',
            users: [
                { value: 'HuaWei', label: '华为(租户)' },
                { value: 'Ali', label: '阿里(租户)' },
                { value: 'XiaoMi', label: '小米(租户)' },
                { value: 'Lenovo', label: '联想(租户)' },
                { value: 'Administrator', label: '宿主' }
            ],
            value: '',
            param: {
                tenantname: '',
                username: 'admin',
                password: '1q2w3E*'
            },
            tenants: ['Ali', 'XiaoMi', 'Lenovo HuaWei'],
            rules: {
                tenantname: [{ required: true, message: ' ', trigger: 'blur' }],
                username: [{ required: true, message: ' ', trigger: 'blur' }],
                password: [{ required: true, message: ' ', trigger: 'blur' }]
            }
        };
    },
    methods: {
        changeUser() {
            this.param.tenantname = this.value;
            if(this.param.tenantname=="Administrator")
               this.param.username = 'administrator';
            else
               this.param.username = this.value+'-admin';  
            this.param.password = '123456';
        },
        submitForm() {


         
            this.$refs.login.validate((valid) => {
                if (valid) {

			axios({
				url: 'https://8.148.82.60:44315/api/product/health',
				method: 'get',
				data: {
					parentId: 0,
					type: 1,
					pageIndex: 1,
					pageSize: 500
				},
				headers: {
					// 'Content-Type': 'multipart/form-data'
				}
			}).then((res) => {
           
                console.log(res);
			});
 
                    //登录获取token

                    // var parmas = {
                    //     userName: this.param.username,
                    //     tenantName: this.param.tenantname,
                    //     passWord: this.param.password
                    // };
   
                    //使用 Axios 发送 application/x-www-form-urlencoded 数据
                    // const params = new URLSearchParams();
                    // params.append('client_id', 'IdentityServer_App');
                    // params.append('client_secret', '1q2w3e*');
                    // params.append('grant_type', 'password');
                    // params.append('userName', 'admin');
                    // params.append('passWord', '1q2w3E*');
         
                    // axios.post('http://8.148.82.60:44310/connect/token', params)
                    // .then(res => {
                    //     console.log(res.data);
                    //         this.$message.success('登录成功!');
                    //         localStorage.setItem('username', params.userName);
                    //         //sessionStorage.setItem('tenantName',this.param.tenantname);
                    //         localStorage.setItem('token', res.data.token);
                    //         config.token = res.data.token;
                    //         window.location.href = '/';
                    // })
                    // .catch(error => {
                    //     console.error('There was an error!', error);
                    // });


//-----------------------------------post常规请求
                    var parmas = {
                        clientId: "IdentityServer_App",
                        clientSecret: "1q2w3e*",
                        tenantId: "446a5211-3d72-4339-9adc-845151f8ada0",
                        grantType: "password",
                        userName: "admin",
                        passWord: "1q2w3E*"
                    };
                    axios({
                       // url: 'http://8.148.82.60:44315/api/identity/user/login',
                        url: 'http://8.148.82.60:44310/api/identity/user/login',
                        //url: config.gatewayUrl + '/auth/LoginPost',
                        method: 'post',
                        data: parmas,
                        // headers: {
                        //     'Content-Type': 'application/x-www-form-urlencoded'//multipart/form-data'
                        // }
                    }).then((res) => {
   
                        if (res.status == 200) {
                            this.$message.success('登录成功!');
                            localStorage.setItem('username', parmas.userName);
         
                            sessionStorage.setItem('tenantName',this.param.tenantname);
                            localStorage.setItem('token', res.data.access_token);
                            config.token = res.data.access_token;
                            window.location.href = '/';

                            //this.$router.replace("/");
                            //this.$router.push('/');
                        } else {
                            console.log(res);
                            this.$message.error(res.data.msg);
                        }
                    });

//----------------------------------------------

                } else {
                    this.$message.error('请输入账号和密码！');
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #888;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>